package com.joysuch.wwyt.bp.service.impl;

import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.bp.entity.BpRewardJobRule;
import com.joysuch.wwyt.bp.entity.BpRewardRules;
import com.joysuch.wwyt.bp.entity.vo.RewardRulesVo;
import com.joysuch.wwyt.bp.mapper.BpRewardRulesMapper;
import com.joysuch.wwyt.bp.repository.BpJobDao;
import com.joysuch.wwyt.bp.service.BpRewardJobRuleService;
import com.joysuch.wwyt.bp.service.BpRewardRulesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.bean.SimpleListBean;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.support.Context;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author heqifeng
 * @since 2022-01-06
 */
@Service
public class BpRewardRulesServiceImpl extends ServiceImpl<BpRewardRulesMapper, BpRewardRules> implements BpRewardRulesService {
    @Autowired
    private BpJobDao jobDao;
    @Autowired
    private BpRewardJobRuleService jobRuleService;
    @Autowired
    private BpRewardRulesMapper mapper;

    @Override
    public Boolean saveOrUpdateRulesBatch(List<RewardRulesVo> rulesVos) {
        checkJobSingle(rulesVos);
        ArrayList<BpRewardRules> rules = new ArrayList<>();
        ArrayList<BpRewardJobRule> jobRuleMapping = new ArrayList<>();
        for (RewardRulesVo rulesVo : rulesVos) {
            if (rulesVo.getId() == null && DeleteFlag.DELETED.getCode().equals(rulesVo.getDeleteFlag())) {
                continue;
            }
            // 带id的做更新
            if (rulesVo.getId() != null) {
                BpRewardRules rule = getById(rulesVo.getId());
                BeanUtils.copyProperties(rulesVo, rule);
                rulesVo.getJobList().forEach(each -> {
                    BpRewardJobRule jobRule = new BpRewardJobRule();
                    jobRule.setJobId(each.getId());
                    jobRule.setRuleId(rule.getId());
                    jobRule.setDeleteFlag(rule.getDeleteFlag());
                    jobRuleMapping.add(jobRule);
                });
                rules.add(rule);
            } else {
                BpRewardRules rule = new BpRewardRules();
                BeanUtils.copyProperties(rulesVo, rule);
                save(rule);
                rulesVo.getJobList().forEach(each -> {
                    BpRewardJobRule jobRule = new BpRewardJobRule();
                    jobRule.setJobId(each.getId());
                    jobRule.setRuleId(rule.getId());
                    jobRuleMapping.add(jobRule);
                });
            }
        }
        jobRuleService.saveOrUpdateBatchByJobId(jobRuleMapping);
        return CollectionUtils.isEmpty(rules) || saveOrUpdateBatch(rules);
    }

    @Override
    public List<RewardRulesVo> getAllRules() {
        List<RewardRulesVo> rewardRulesVos = mapper.selectAll(Context.getCompanyCode(), Context.getCurrentTenantId());
        mergeJobName(rewardRulesVos);
        return rewardRulesVos;
    }

    public void mergeJobName(List<RewardRulesVo> allRule) {
        if (!CollectionUtils.isEmpty(allRule)) {
            allRule.forEach(each -> {
                String jobNames = each.getJobList().stream().map(SimpleListBean::getName).collect(Collectors.joining(Constant.SPLIT_COMMA));
                each.setJobNames(jobNames);
            });
        }
    }

    public void checkJobSingle(List<RewardRulesVo> rulesVos) {
        List<List<SimpleListBean>> jobLists = rulesVos.stream()
                .filter(each -> DeleteFlag.EFFECTIVE.getCode().equals(each.getDeleteFlag()))
                .map(RewardRulesVo::getJobList).collect(Collectors.toList());
        List<SimpleListBean> allJob = new ArrayList<>();
        jobLists.forEach(each -> allJob.addAll(each));
        Map<Long, List<SimpleListBean>> jobMap = allJob.stream().collect(Collectors.groupingBy(SimpleListBean::getId));
        List<String> sameJobNames = new ArrayList<>();
        jobMap.forEach((k,v) ->{
            if (v.size() > 1) {
                sameJobNames.add(v.get(0).getName());
            }
        });
        if (sameJobNames.size() > 0) {
            throw new IllegalArgumentException(String.format(Status.JOB_BIND_RULE_FIELD.getMsg(),sameJobNames.stream().collect(Collectors.joining(Constant.SPLIT_STOP))));
        }
    }

}
